package com.sinping.qh.api.admin.common;



import com.sinping.qh.api.admin.base.BaseAdminEndPoint;
import com.sinping.qh.api.app.base.BaseAppEndPoint;
import com.sinping.qh.api.support.Const;
import com.sinping.qh.api.support.ConstEum;
import com.sinping.qh.api.support.FrontPage;
import com.sinping.qh.api.support.ResDto;
import com.sinping.qh.domain.user.MessageInfoDomain;
import com.sinping.qh.dto.admin.user.MessageInfoDto;
import com.sinping.qh.dto.auth.UserSessionDTO;
import com.sinping.qh.repository.mongo.MessageInfoRepository;
import com.sinping.qh.service.app.MessageInfo.IMessageInfoService;
import com.sinping.qh.service.auth.ISessionService;
import com.sinping.qh.utils.mapper.BeanMapper;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(value = "admin通知消息管理",description = "admin通知消息管理API")
@RequestMapping(value = Const.ADMIN_URI+"message_info",name = "通知消息管理")
@RestController
public class MessageInfoEndPoint extends BaseAdminEndPoint {

    @Autowired
    MessageInfoRepository messageInfoRepository;

    @Autowired
    private IMessageInfoService iMessageInfoService;

    @Autowired
    private ISessionService iSessionService;

    @ApiOperation(value = "新增通知消息")
    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResDto addMessageInfo(HttpServletRequest httpServletRequest,@RequestParam(required = true) @ApiParam(name = "msg",value = "{\"title\":\"这是一条通知\",\"content\":\"这是通知内容\",\"type\":\"0\",\"doc_id\":\"0\",\"receive_ids\":[\"1\",\"23\"]}") String msg){

        MessageInfoDto messageInfoDto = JsonMapper.defaultMapper().fromJson(msg,MessageInfoDto.class);
        checkNull(messageInfoDto,ConstEum.PARAM_ERROR);


        ResDto resDto = iMessageInfoService.addMessageInfo(httpServletRequest,messageInfoDto);
        return resDto;

    }

    @ApiOperation(value = "更新通知消息")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ResDto UpdateMessageInfo(HttpServletRequest httpServletRequest,@RequestParam(required = true) @ApiParam(name = "msg",value = "{\"id\":\"123\",\"receive_ids\":[\"1\",\"23\"]}") String msg){

        MessageInfoDto messageInfoDto = JsonMapper.defaultMapper().fromJson(msg,MessageInfoDto.class);
        checkNull(messageInfoDto,ConstEum.PARAM_ERROR);

        ResDto resDto = iMessageInfoService.updateMessageInfo(httpServletRequest,messageInfoDto);
        return resDto;

    }

    @ApiOperation(value = "删除通知消息")
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public ResDto deleteMessageInfoById(@ApiParam(name = "id",value = "123") @PathVariable String id){

        if (StringUtils.isBlank(id)){
            return new ResDto(Const.PARAM_ERROR,"id不能为空");
        }
        ResDto resDto = iMessageInfoService.deleteMessageInfoById(id);
        return resDto;
    }

    @ApiOperation(value = "获取通知消息详情")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ResDto getMessageInfoById(@ApiParam(name = "id", value = "123") @PathVariable String id){

        if (StringUtils.isBlank(id)){
            return new ResDto(Const.PARAM_ERROR,"id不能为空");
        }
        ResDto resDto = iMessageInfoService.getMessageInfoById(id);
        return resDto;
    }

    @ApiOperation(value = "获取通知消息分页列表")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResDto getMessageInfoFrontPage(HttpServletRequest httpServletRequest,@ApiParam(name = "msg",value = "{\"page_size\":10, \"page_current\":1,\"condition\":{\"title\":\"123\",\"type\":\"0\",\"start_time\":\"2018-06-05 00:00:00\",\"end_time\":\"2018-06-08 00:00:00\"}}") @RequestParam(required = true)  String msg){
        FrontPage<MessageInfoDto> frontPage = JsonMapper.defaultMapper().fromJson(msg,FrontPage.class);
        checkNull(frontPage,ConstEum.PARAM_ERROR);

        String token =httpServletRequest.getHeader(Const.TOKEN_NAME);
        UserSessionDTO userSessionDTO = iSessionService.getSessionByToken(token);
        if (userSessionDTO == null){
            return new ResDto(Const.PARAM_ERROR,"用户未登录");
        }

        if (org.apache.commons.lang.StringUtils.isBlank(userSessionDTO.getId())){
            return new ResDto(Const.SERVER_ERROR,"获取用户信息错误");
        }

        String userId = userSessionDTO.getId();

        ResDto resDto = iMessageInfoService.getMessageInfoFrontPage(userId,frontPage);
        return resDto;
    }

    @ApiOperation(value = "根据文档Id获取通知消息")
    @RequestMapping(value = "/doc_msg/{id}",method = RequestMethod.GET)
    public ResDto getMessageInfoByDocId(@ApiParam(name = "id", value = "123") @PathVariable String id){

        if (StringUtils.isBlank(id)){
            return new ResDto(Const.PARAM_ERROR,"doc_id不能为空");
        }
        ResDto resDto = new ResDto();
        MessageInfoDomain messageInfoDomain = messageInfoRepository.findMessageInfoDomainByDocId(id);
        if (messageInfoDomain == null){
            resDto.setStatus(Const.DATA_ERROR);
            resDto.setMsg("没有该记录");
            return resDto;
        }
        MessageInfoDto messageInfoDto = BeanMapper.map(messageInfoDomain,MessageInfoDto.class);
        String json = JsonMapper.defaultMapper().toJson(messageInfoDto);
        resDto.setDatas(json);
        resDto.setMsg("成功");
        resDto.setStatus(Const.SUCCESS);
        return resDto;
    }


    @ApiOperation(value = "获取消息发送分页列表")
    @RequestMapping(value = "/send_list",method = RequestMethod.GET)
    public ResDto getMessageSendList(@ApiParam(name = "msg",value = "{\"page_size\":10,\"page_current\":1,\"id\":\"518e0b51db644078887070c63f3bc8b7\",\n\"send_type\":\"1\",\"full_name\":\"刘\",\"unit_id\":\"861a3f3133944c70971161f5b09267da\",\n\"role_id\":\"669fd01bebae11e7a72bf079598c8c56\"}") @RequestParam(required = true)  String msg){
        ResDto resDto = iMessageInfoService.getMessageInfoByMsg(msg);
        return resDto;
    }

}
